package vip.meeet.thread;

import java.util.concurrent.*;

/**
 * 创建线程3种方式：
 * 1. 继承Thread
 * 2. 实现Runable,RunnableFuture
 * 3. 实现Callable,结合FutureTask(RunnableFuture)，本质上还是Runnable
 *
 * Future:CompletableFuture
 * CompletableFutureTest
 */
public class CallableStart {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CallableTask callableTask = new CallableTask();
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future future = executorService.submit(callableTask);
        System.out.println(future.get());
    }

    private static void callable() throws ExecutionException, InterruptedException {
        CallableTask callableTask = new CallableTask();
        FutureTask<Integer> ft = new FutureTask<>(callableTask);
        Thread callableThread = new Thread(ft, "有返回值的线程");
        callableThread.start();
        System.out.println(ft.get());
    }

    private static class CallableTask implements Callable {
        @Override
        public Integer call() {
            int i = 0;
            for (; i < 100; i++) {
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
            return i;
        }
    }
}
